package com.itheima.condition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class ClassCondition implements Condition {

    /**
     *
     * @param conditionContext  上下文对象，获取Bean工厂，获取ClassLoader
     * @param annotatedTypeMetadata  注解元对象，用于获取注解信息
     * @return
     */

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //返回是false时，Bean不加载的
        //返回是true时，Bean加载的

        Map<String, Object> map = annotatedTypeMetadata.getAnnotationAttributes(MyConditionalOnClass.class.getName());
        System.out.println(map);

        boolean flag = true;
        try {
            String[] strings = (String[]) map.get("value");
            for (String className : strings) {
                Class.forName(className);
            }
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;
    }
}
